<template>
<el-container>
    
<div>
<div class="formCss">
  <el-table :data="schedules" highlight-current-row @current-change="handleCurrentChange" style="width: 100%">
    <el-table-column label="序号" type="index" width="50"></el-table-column>
    <el-table-column property="event" label="日程名称" width="120"></el-table-column>
    <el-table-column property="startTime" label="日程开始时间" width="120"></el-table-column>
    <el-table-column property="endTime" label="日程结束时间" width="120"></el-table-column>
     <el-table-column label="操作">
      <template #default="scope">
        <el-button
          size="mini"
          type="danger"
          @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<div class="formCss">
  <span class="timeCss">{{message}}</span>
<el-form ref="schedule" :model="schedules" label-width="80px">
  <el-form-item label="日程名称" class="eventCss">
    <el-input ref="event" placeholder="请输入日程名称" v-model="schedule.event"></el-input><span class="errCss">{{error.event}}</span>
  </el-form-item>
   <el-form-item label="日程时间" class="labCss">
    <el-col :span="11"  class="dateCss">
      <el-date-picker ref="startTime" value-format="YYYY-MM-DD"  type="date" placeholder="开始时间" v-model="schedule.startTime" style="width: 100%;"></el-date-picker><span class="errCss">{{error.startTime}}</span>
    </el-col>
    <el-col :span="2" class="line">-</el-col>
    <el-col :span="11"  class="dateCss">
      <el-date-picker ref="endTime" value-format="YYYY-MM-DD"  type="date" placeholder="结束时间" v-model="schedule.endTime" style="width: 100%;"></el-date-picker><span class="errCss">{{error.endTime}}</span>
    </el-col>
  </el-form-item>
  <el-form-item>
      <div class="butCss"><el-button class="onSubmit" type="primary" @click="onSubmit">创建日程</el-button></div>  
  </el-form-item>
</el-form>
</div>

</div>
    
</el-container>
</template>

<script>
import CompanyMenu from '@/components/menu/CompanyMenu.vue'
import instance from '@/utlis/request.js'

export default {
    
    data() {
        
        return {
        schedules:[],
        sel:"",
        message:"",
        schedule: {
          event:"",
          startTime:"",
          endTime:""         
          
        },
        error:{
               event:"",
               startTime:"",
               endTime:""  
          } 
        };
    },
    //钩子函数   一般用来初始化数据
    created:function(){
      this.initData();
    },
     methods: {
      //显示页面所有的日程功能
      initData(){
        var userinfo=window.sessionStorage.getItem("loginUser");
        var userinfoObj = JSON.parse(userinfo);
        var id = userinfoObj.id;
        instance.get("Web/schedule/listSchedule/"+id).then(res=>{
          this.schedules=res.data.data;
        })
      },
       //创建日程的功能
      onSubmit() {
        
          //日程名称格式判断
          if(this.schedule.event==undefined||this.schedule.event.trim()==""){
            this.error.event="日程名称不能为空";
            this.$refs.event.select();
            return;
          }else{
              this.error.event="";
          }
          //开始时间格式判断
          if(this.schedule.startTime==undefined||this.schedule.startTime==""){
              this.error.startTime="开始时间不能为空";
              this.$refs.startTime.focus();
              return;
          }else{
              this.error.startTime="";
          }
          //结束时间格式判断
          if(this.schedule.endTime==undefined||this.schedule.endTime==""){
              this.error.endTime="结束时间不能为空";
              this.$refs.endTime.focus();
              return;
          }else{
              this.error.endTime="";
          }
           //判断日程时间(结束时间不能小于开始时间)
           let date1 = new Date(this.schedule.startTime).getTime()
           //1646697600000
           console.log(date1);
           let date2 = new Date(this.schedule.endTime).getTime()
           //1647216000000
           console.log(date2);
           if(date1>date2){
              this.message="结束时间不能小于开始时间";
              return;
           }else{
             this.message=""
           }
           
            var userinfo=window.sessionStorage.getItem("loginUser");
            var userinfoObj = JSON.parse(userinfo);
            this.schedule.userinfoId=userinfoObj.id
           instance.post("Web/schedule/insertSchedule",this.schedule
          ).then(res=>{
               this.$router.push({path:"/CalendarView"});
               this.$router.go(0);
           });
      },
      //删除日程的功能
      handleDelete(index,row){
        var del=confirm("您确定删除该日程吗?");
        if(del==true){
        instance.get("Web/schedule/deleteSchedule",{
          params:{id:row.id}
        }).then(res=>{
          this.schedules.splice(index,1);
        });
       }
      },
      //修改日程
      handleEdit(index,row){
        this.$emit('edit',row)

      }
     
    }

}
</script>
<style scoped>
    .eventCss{
        margin-top: 100px;
        width: 720px;   
    }
    .dateCss{
        width: 320px;
    }
    .labCss{
       margin-top: 30px; 
    }
    .formCss{
         margin-left: 380px;
    }
    .butCss{
        margin-left: 260px;
        margin-top: 20px;
    }
    .errCss{
        color:red;
    }
   .timeCss{
      color:red;
   }
   .onSubmit{
     margin-right: 340px;
   }
   
</style>